<template>
  <LoginHeader>QQ联合登陆</LoginHeader>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a href="javascript:;" @click="hasAccount = true" :class="{ active: hasAccount == true }">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a href="javascript:;" @click="hasAccount = false" :class="{ active: hasAccount == false }">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId"></CallbackBind>
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId"></CallbackPatch>
    </div>
  </section>
  <LoginFooter></LoginFooter>
</template>

<script>
import { ref } from 'vue'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import Message from '@/components/library/Message'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { userQQLogin } from '@/api/user'
import QC from 'qc'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'LoginCallback',
  components: {
    LoginHeader,
    LoginFooter,
    CallbackBind,
    CallbackPatch
  },
  setup() {
    const hasAccount = ref(true)
    //首先默认以及注册及已经绑定
    //通过QQ的API获取openId就是后台需要的unionId然后去登陆
    //如果成功：跳转首页或者之前的页面
    //如果失败：该QQ为和小兔鲜进行绑定（有账号为绑定QQ，没账号未绑定QQ）
    const store = useStore()
    const router = useRouter()

    const unionId = ref(null)

    const isBind = ref(true)
    //确保QQ已经登陆
    if (QC.Login.check()) {
      //openId就是第三方的唯一标识（QQ的唯一标识）
      QC.Login.getMe((openId) => {
        unionId.value = openId
        //请求小兔鲜后台 做QQ登陆
        userQQLogin(openId).then((data) => {
          //登陆成功：data.result就是用户的信息对象
          //1.存储用户信息
          const { id, account, nickname, avatar, token, mobile } = data.result
          store.commit('user/setUser', { id, account, nickname, avatar, token, mobile })
          store.dispatch('cart/mergeLocalCart').then(() => {
            //2.跳转到来源页或首页
            router.push(store.state.user.redirectUrl)
            //3.成功提示
            Message({ type: 'success', text: 'QQ登陆成功' })
          })

        }).catch((e) => {
          //登陆失败：没有和小兔鲜绑定
          isBind.value = false
        })
      })
    }
    return {
      hasAccount,
      isBind,
      unionId
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 1.5625rem 0;
  position: relative;
  height: 45.625rem;

  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 1.5625rem 0;
    z-index: 99;

    .loading {
      height: 100%;
      background: #fff url(../../assets/image/load.gif) no-repeat center / 6.25rem 6.25rem;
    }
  }
}

.tab {
  background: #fff;
  height: 5rem;
  padding-top: 2.5rem;
  font-size: 1.125rem;
  text-align: center;

  a {
    color: #666;
    display: inline-block;
    width: 21.875rem;
    line-height: 2.5rem;
    border-bottom: .125rem solid #e4e4e4;

    i {
      font-size: 1.375rem;
      vertical-align: middle;
    }

    span {
      vertical-align: middle;
      margin-left: .25rem;
    }

    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}

.tab-content {
  min-height: 37.5rem;
  background: #fff;
}
</style>